package com.leostadyn.stackfarm.timestatistics.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.model.bean.DetailProjectBean;

import java.util.List;

public class MainProjectAdapter extends RecyclerView.Adapter<MainProjectAdapter.VH> {
    private List<DetailProjectBean> list;
    private OnRecyclerItemClickListener mItemClickListener;

    public MainProjectAdapter(@NonNull List<DetailProjectBean> list) {
        this.list = list;
    }

    public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
        mItemClickListener = listener;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_admin_main_project, viewGroup, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VH vh, int i) {
        vh.nameTxt.setText(list.get(i).getProjectName());
        vh.numberTxt.setText("编号：" + list.get(i).getProjectCode());
        vh.dayTxt.setText(list.get(i).getProjectWorkingHours() + "天");


    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public interface OnRecyclerItemClickListener {
        //RecyclerView的点击事件，将信息回调给view
        void onItemClick(int Position, List<DetailProjectBean> dataBeanList);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class VH extends RecyclerView.ViewHolder {
        TextView nameTxt;
        TextView numberTxt;
        TextView dayTxt;

        public VH(@NonNull View itemView) {
            super(itemView);
            nameTxt = itemView.findViewById(R.id.txt_project_name);
            numberTxt = itemView.findViewById(R.id.txt_project_number);
            dayTxt = itemView.findViewById(R.id.txt_project_day);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null)
                        mItemClickListener.onItemClick(getAdapterPosition(), list);
                }
            });
        }
    }

}
